package com.example.test.fragment;

import com.example.test.LoginActivity;
import com.example.test.MainActivity;
import com.example.test.R;
import com.example.test.RegActivity;
import com.example.test.utils.DbDaoUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Paint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class LoginFragment extends Fragment implements OnClickListener {

	private Context ct;
    @ViewInject(R.id.bt_login)
    private Button btn_login;
    @ViewInject(R.id.reg_text)
    private TextView reg_user;
    @ViewInject(R.id.et_uername)
    private EditText et_username;
    @ViewInject(R.id.et_password)
    private EditText et_password;
	private String uername;
	private String password;
	private Intent intent;
	private SharedPreferences sp;
 
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		ct=getActivity();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View view=inflater.inflate(R.layout.login_layout, null);
		ViewUtils.inject(this,view);
		init();
		return view;
	}

	/**
	 * 
	 */
	private void init() {
		
		reg_user.setOnClickListener(this);
				// TODO Auto-generated method stub
				
		reg_user.setText(Html.fromHtml("<u>"+getString(R.string.reg_newuser)+"</u>"));
		btn_login.setOnClickListener(this);
				// TODO Auto-generated method stub
				

	}
	private boolean checkForm() {
		// TODO Auto-generated method stub
		uername = et_username.getEditableText().toString().trim();
		password = et_password.getEditableText().toString().trim();
		
		
		
       if(TextUtils.isEmpty(uername)){
    	   Toast.makeText(ct, "账号不能为空", Toast.LENGTH_SHORT).show();
    	   return false;
       }
       if(TextUtils.isEmpty(password)){
    	   Toast.makeText(ct, "密码不能为空", Toast.LENGTH_SHORT).show();
    	   return false;
       }
       
		
		return true;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.reg_text:
			intent = new Intent(ct, RegActivity.class);
			startActivity(intent);
			break;

		case R.id.bt_login:
			if(checkForm()){
				DbDaoUtils daoUtils=new DbDaoUtils(ct);
				boolean res = daoUtils.findUser(uername, password);
				if(res){
					sp = ct.getSharedPreferences("user", Context.MODE_PRIVATE);
					Editor editor = sp.edit();
					editor.putBoolean("login", true);
					editor.putString("username", uername);
					editor.commit();
					Toast.makeText(ct, "登陆成功", 0).show();
					intent=new Intent(ct, MainActivity.class);
					startActivity(intent);
					((LoginActivity)ct).finish();
				}
				else{
					Toast.makeText(ct, "用户名或密码错误", 0).show();
				}
			}
		
			break;
		default:
			break;
		}
	}

}
